package com.yunwaikeji.gy_tool_library.mvvm.view.activity

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.MotionEvent
import android.view.View
import com.weikaiyun.fragmentation.SupportActivity
import com.yunwaikeji.gy_tool_library.info.GYSmartRefreshInfo
import com.yunwaikeji.gy_tool_library.mvvm.view.admin.GYViewAdmin
import com.yunwaikeji.gy_tool_library.utils.GYKeyboardUtils

abstract class GYActivity : SupportActivity(),
    GYActivityInterface, GYSmartRefreshInfo {
    val mViewAdmin = GYViewAdmin.newInstance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewAdmin.create(this, getLayoutId())
        setContentView(mViewAdmin.view)
        mViewAdmin.loadingLayout.setOnErrorTextClickListener {
            showLayoutLoading()
            initData(true)
        }
        initDataObserver(this)
        initView(savedInstanceState)
        Handler(Looper.getMainLooper()).postDelayed({
            initViewDelayed(savedInstanceState)
            initData(true)
        }, mViewAdmin.initViewDelayed)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return GYKeyboardUtils.getInstance().setOnOutsideHideSoftInput(this, ev)
    }

    override fun showTopBar() {
        showTopBar(true)
    }

    override fun showTopBar(show: Boolean) {
        if (show) mViewAdmin.topBar.visibility = View.VISIBLE else mViewAdmin.topBar.visibility =
            View.GONE
    }

    override fun showLoadingDialog() {
        mViewAdmin.loadingDialog.show()
    }

    override fun showLoadingDialog(text: String?) {
        mViewAdmin.loadingDialog.setTipText(text)
        mViewAdmin.loadingDialog.show()
    }

    override fun dismissLoadingDialog() {
        mViewAdmin.loadingDialog.dismiss()
    }

    override fun showLayoutContent() {
        mViewAdmin.loadingLayout.showContent()
    }

    override fun showLayoutLoading() {
        mViewAdmin.loadingLayout.showLoading()
    }

    override fun showLayoutLoading(text: String?) {
        mViewAdmin.loadingLayout.showLoading(text)
    }

    override fun showLayoutError() {
        mViewAdmin.showError("加载失败")
    }

    override fun showLayoutError(text: String?) {
        mViewAdmin.loadingLayout.showError(text)
    }
}